package com.wzq.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wzq.app.R;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseActivity;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.UserInfo;
import com.wzq.app.utils.LaunchUtils;
import com.wzq.app.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_password)
    EditText loginPassword;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.register)
    TextView register;


    @Override
    public int getLayoutId() {
        return R.layout.login_activity;
    }

    @Override
    public void initData() {
    }

    @Override
    public void configView() {
        UserManager.getInstance().clear();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext,RegisterActivity.class));
            }
        });
    }


    @Override
    public void requestData() {

    }


    private void login() {
        if (TextUtils.isEmpty(loginPhone.getText())) {
            ToastUtils.showToast(mContext, "请输入用户名");
            loginPhone.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(loginPassword.getText())) {
            ToastUtils.showToast(mContext, "请输入密码");
            loginPassword.requestFocus();
            return;
        }
        Call<BaseModel<UserInfo>> call = Api.getInstance().service.login(loginPhone.getText().toString().trim(), loginPassword.getText().toString().trim());
        call.enqueue(new Callback<BaseModel<UserInfo>>() {
            @Override
            public void onResponse(Call<BaseModel<UserInfo>> call, Response<BaseModel<UserInfo>> response) {
                BaseModel<UserInfo> model = response.body();
                if (response.isSuccessful()){
                    if (model.code == ApiResponseCode.RESPHONE_OK) {
                        UserManager.getInstance().saveUser(model.obj);
                        LaunchUtils.launchMain(mContext);
                        finish();
                    } else {
                        ToastUtils.showToast(mContext, model.msg);
                    }
                }else {
                    ToastUtils.showToast(mContext, "登陆失败，请检查网络");
                }
            }

            @Override
            public void onFailure(Call<BaseModel<UserInfo>> call, Throwable throwable) {
                ToastUtils.showToast(mContext, "登陆失败，请检查网络");
            }
        });
        putCall(call);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}

